<?php
	require_once("include/jara_config.php");
	if(!defined("JARA_NOT_INSTALLED")) {
		header("x", true, 404);
		exit;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Jara Installer</title>
	<style type="text/css">
	<!--
		body {
			font: 11px Verdana, sans-serif;
		}
		.passed {
			color: #070;
			font-weight: bolder;
		}
		.failed {
			color: #c00;
			font-weight: bolder;
		}
	-->
	</style>
</head>
<body>
<h1>Jara Installer</h1>
<?php
	$step = 1;
	if(isset($_REQUEST["step"])) {
		$step = $_REQUEST["step"];
	}
	if($step == 1) {
		clearstatcache();
		$check1 = ((file_exists("docs/config.txt")) && (file_exists("docs/sql.txt")));
		$check2 = (is_writable("include/jara_config.php"));
		$check3 = (is_writable("uploads/"));
		$check4 = (extension_loaded("mysqli"));
?>
<h2>Step 1: Pre-Installation Checks</h2>
<table border="0">
	<tr>
		<td><strong>Installation Files</strong></td>
		<td><span class="<?php if($check1) { echo "passed"; } else { echo "failed"; } ?>"><?php if($check1) { echo "PASSED"; } else { echo "FAILED"; } ?></span></td>
		<td><?php if(!$check1) { ?>One of the installer files (docs/config.txt or docs/sql.txt) is missing or not available. Please re-extract the Jara files.<?php } ?></td>
	</tr>
	<tr>
		<td><strong>Configuration File Writable</strong></td>
		<td><span class="<?php if($check2) { echo "passed"; } else { echo "failed"; } ?>"><?php if($check2) { echo "PASSED"; } else { echo "FAILED"; } ?></span></td>
		<td><?php if(!$check2) { ?>Please make sure the include/jara_config.php file is writable.<?php } ?></td>
	</tr>
	<tr>
		<td><strong>Uploads Directory Writable</strong></td>
		<td><span class="<?php if($check3) { echo "passed"; } else { echo "failed"; } ?>"><?php if($check3) { echo "PASSED"; } else { echo "FAILED"; } ?></span></td>
		<td><?php if(!$check3) { ?>Please make sure the uploads/ directory is writable.<?php } ?></td>
	</tr>
	<tr>
		<td><strong>MySQLi Extension Available</strong></td>
		<td><span class="<?php if($check4) { echo "passed"; } else { echo "failed"; } ?>"><?php if($check4) { echo "PASSED"; } else { echo "FAILED"; } ?></span></td>
		<td><?php if(!$check4) { ?>Please make sure the MySQLi extension is configured and enabled.<?php } ?></td>
	</tr>
</table>
<?php
		if($check1 && $check2 && $check3 && $check4) {
?>
	<p><input type="button" onclick="window.location.href='?step=2'" value="Next Step &gt;&gt;" /></p>
<?php
		}
		else {
?>
	<p>This installer cannot function with even a single test incorrect. Please double-check your permissions and make sure MySQLi is definitely installed (do other scripts work?)</p>
<?php
		}
	}
	else if($step == 2) {
?>
<h2>Step 2: Input Configuration</h2>
<form action="?step=3" method="post">
	<input type="hidden" name="step" id="step" value="3" />
	<table border="0">
		<tr>
			<td colspan="2" align="center"><strong>MySQL Details</strong></td>
		</tr>
		<tr>
			<td>MySQL Hostname</td>
			<td><input type="text" name="mysql_hostname" id="mysql_hostname" /></td>
		</tr>
		<tr>
			<td>MySQL Username</td>
			<td><input type="text" name="mysql_username" id="mysql_username" /></td>
		</tr>
		<tr>
			<td>MySQL Password</td>
			<td><input type="password" name="mysql_password" id="mysql_password" /></td>
		</tr>
		<tr>
			<td>MySQL Database</td>
			<td><input type="text" name="mysql_database" id="mysql_database" /></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td colspan="2" align="center"><strong>Administrator Details</strong></td>
		</tr>
		<tr>
			<td>Administrator Username</td>
			<td><input type="text" name="su_username" id="su_username" /></td>
		</tr>
		<tr>
			<td>Administrator Password</td>
			<td><input type="password" name="su_password" id="su_password" /></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td colspan="2" align="center"><strong>Blog Details</strong></td>
		</tr>
		<tr>
			<td>Blog Title</td>
			<td><input type="text" name="blog_title" id="blog_title" /></td>
		</tr>
		<tr>
			<td>Blog URL</td>
			<td><input type="text" name="blog_url" id="blog_url" value="http://<?php echo $_SERVER["HTTP_HOST"]; ?><?php echo str_replace("/_install.php?step=2", "", $_SERVER["REQUEST_URI"]); ?>"/></td>
			<td>Please <strong>do not</strong> add a trailing slash.</td>
		</tr>
	</table>
	<p><input type="button" onclick="window.location.href='?step=1'" value="&lt;&lt; Previous Step" /><input type="submit" value="Install &gt;&gt;" /></p>
</form>
<?php
	}
	else if($step == 3) {
		$finished = true;
		$mysql_hostname = $_REQUEST["mysql_hostname"];
		$mysql_username = $_REQUEST["mysql_username"];
		$mysql_password = $_REQUEST["mysql_password"];
		$mysql_database = $_REQUEST["mysql_database"];
		$su_username = $_REQUEST["su_username"];
		$su_password = $_REQUEST["su_password"];
		$blog_title = $_REQUEST["blog_title"];
		$blog_url = $_REQUEST["blog_url"];
?>
<h2>Step 3: Install Jara</h2>
<p>
Creating table structure...<br />
<?php
		$db = new mysqli($mysql_hostname, $mysql_username, $mysql_password, $mysql_database);
		if(mysqli_connect_errno()) {
			echo "There was a problem connecting to the database: <strong>".mysqli_connect_error()."</strong>. Please double-check your connection settings.";
			$finished = false;
		}
		else {
			$v = file_get_contents("docs/sql.txt");
			$v = str_replace("<<USERNAME>>", $su_username, $v);
			$v = str_replace("<<PASSWORD>>", $su_password, $v);
			$v = str_replace("<<TITLE>>", $blog_title, $v);
			$v = str_replace("<<URL>>", $blog_url, $v);
			$db->multi_query($v);
			if(!$db->errno) {
				echo "Table structure created.";
			}
			else {
				echo "Couldn't create the table structure: <strong>".$db->error."</strong>.";
				$finished = false;
			}
			$db->close();
		}		
?>
</p>
<p>
Writing configuration file...<br />
<?php
		$v = file_get_contents("docs/config.txt");
		$v = str_replace("<<hostname>>", $mysql_hostname, $v);
		$v = str_replace("<<username>>", $mysql_username, $v);
		$v = str_replace("<<password>>", $mysql_password, $v);
		$v = str_replace("<<database>>", $mysql_database, $v);
		file_put_contents("include/jara_config.php", $v);
		echo "Configuration file written.";
?>
</p>
<p>
<?php
		if($finished) { 
?>
Jara has been installed! Please delete this file after the installation is finished.<br />
<a href="index.php">Visit my blog</a>
<?php
		}
		else {
?>
There were problems installing Jara.<br />
Please try <a href="?step=2">going back</a> and re-entering your configuration.
<?php
		}
	}
?>
</body>
</html>